package com.Zhengxin.smarthome;

import java.util.ArrayList;
import java.util.Calendar;

import com.Zhengxin.bean.AlarmDB;
import com.Zhengxin.bean.FindNewAlarm;
import com.Zhengxin.bean.SetAlarm;
import com.Zhengxin.smarthome.db.DatabaseHelper;
import com.Zhengxin.smarthome.db.DatabaseOpenHelper;

import android.R.color;
import android.R.integer;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.TimePickerDialog;
import android.content.Intent;
import android.content.res.ColorStateList;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker.OnTimeChangedListener;

public class Alarm extends Activity implements OnCheckedChangeListener {
	
	
	private Button sunday;
	private Button monday;
	private Button tuesday;
	private Button wednesday;
	private Button turesday;
	private Button Firday;
	private Button Saturday;
	
	private Button Addalarm;
	private Button delAlarm;
	
	private CheckBox mCheck1;
	private CheckBox mCheck2;
	private CheckBox mCheck3;
	private CheckBox mCheck4;
	
	private TimePicker TimePicker01;
	
	private int buttonSU;
	private int buttonMO;
	private int buttonTUE;
	private int buttonWEN;
	private int buttonTUR;
	private int buttonFIR;
	private int buttonSA;
	
	private int check1=0;
	private int check2=0;
	private int check3=0;
	private int check4=0;
	
	private int mHours=0;
	private int mMinute=0;
	
	private String chackweek;
	private String chackequip;
	
	
	private int id;
	private String time;
	private String week;
	private String equipment;
	
//	DatabaseHelper dbHelper= new DatabaseHelper(Alarm.this);
	
	//private String Time;
	//private int mDay;
	
	private Calendar c= Calendar.getInstance();
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.alarm);
        
        Bundle b = this.getIntent().getExtras();
        id=b.getInt("ID");
        time=b.getString("Time");
        week=b.getString("Week");
        equipment=b.getString("Equip");
        //System.out.println("get equipment"+equipment);
        
//        String[] mTime=time.split(":");
        //String whereId="ID="+id;
        
		//System.out.println("ID is "+id);
//        time=b.getString("Time");
//        week=b.getString("Week");
//        equipment=b.getString("Equip");
//        
        //Char to String to int
		buttonMO=Integer.parseInt(String.valueOf(week.charAt(0)));
		buttonTUE=Integer.parseInt(String.valueOf(week.charAt(1)));
		buttonWEN=Integer.parseInt(String.valueOf(week.charAt(2)));
		buttonTUR=Integer.parseInt(String.valueOf(week.charAt(3)));
		buttonFIR=Integer.parseInt(String.valueOf(week.charAt(4)));
		buttonSA=Integer.parseInt(String.valueOf(week.charAt(5)));
		buttonSU=Integer.parseInt(String.valueOf(week.charAt(6)));
		
		System.out.println("get time"+time);
        
        
        sunday=(Button)findViewById(R.id.Sunday);
        if(buttonSU==0){
        	sunday.setBackgroundResource(R.drawable.up);
        }
        else{
        	sunday.setBackgroundResource(R.drawable.down);
        }
        sunday.setText("SU");
        sunday.setTextColor(Color.RED);
        sunday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonSU==0){
					buttonSU=7;
					sunday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					sunday.setBackgroundResource(R.drawable.up);
					buttonSU=0;
				}
			}
		});
        
        monday=(Button)findViewById(R.id.monday);
        if(buttonMO==0){
        	monday.setBackgroundResource(R.drawable.up);
        }
        else{
        	monday.setBackgroundResource(R.drawable.down);
        }
        //monday.setBackgroundResource(R.drawable.up);
        monday.setText("MO");
        monday.setTextColor(Color.BLACK);
        monday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonMO==0){
					buttonMO=1;
					monday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					monday.setBackgroundResource(R.drawable.up);
					buttonMO=0;
				}
			}
		});
        
        tuesday=(Button)findViewById(R.id.tuesday);
        if(buttonTUE==0){
        	tuesday.setBackgroundResource(R.drawable.up);
        }
        else{
        	tuesday.setBackgroundResource(R.drawable.down);
        }
        //tuesday.setBackgroundResource(R.drawable.up);
        tuesday.setText("TU");
        tuesday.setTextColor(Color.BLACK);
        tuesday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonTUE==0){
					buttonTUE=2;
					tuesday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					tuesday.setBackgroundResource(R.drawable.up);
					buttonTUE=0;
				}
			}
		});
        
        wednesday=(Button)findViewById(R.id.wednesday);
        if(buttonWEN==0){
        	wednesday.setBackgroundResource(R.drawable.up);
        }
        else{
        	wednesday.setBackgroundResource(R.drawable.down);
        }
        wednesday.setText("WE");
        wednesday.setTextColor(Color.BLACK);
        wednesday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonWEN==0){
					buttonWEN=3;
					wednesday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					wednesday.setBackgroundResource(R.drawable.up);
					buttonWEN=0;
				}
			}
		});
        
        turesday=(Button)findViewById(R.id.Turesday);
        if(buttonTUR==0){
        	turesday.setBackgroundResource(R.drawable.up);
        }
        else{
        	turesday.setBackgroundResource(R.drawable.down);
        }
        turesday.setText("TU");
        turesday.setTextColor(Color.BLACK);
        turesday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonTUR==0){
					buttonTUR=4;
					turesday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					turesday.setBackgroundResource(R.drawable.up);
					buttonTUR=0;
				}
			}
		});
        
        Firday=(Button)findViewById(R.id.Firday);
        if(buttonFIR==0){
        	Firday.setBackgroundResource(R.drawable.up);
        }
        else{
        	Firday.setBackgroundResource(R.drawable.down);
        }
        Firday.setText("FI");
        Firday.setTextColor(Color.BLACK);
        Firday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonFIR==0){
					buttonFIR=5;
					Firday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					Firday.setBackgroundResource(R.drawable.up);
					buttonFIR=0;
				}
			}
		});
        
        Saturday=(Button)findViewById(R.id.Saturday);
        if(buttonSA==0){
        	Saturday.setBackgroundResource(R.drawable.up);
        }
        else{
        	Saturday.setBackgroundResource(R.drawable.down);
        }
        Saturday.setText("SA");
        Saturday.setTextColor(Color.BLACK);
        Saturday.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(buttonSA==0){
					buttonSA=6;
					Saturday.setBackgroundResource(R.drawable.down);
					//System.out.println(buttonSU);
				}
				else {
					Saturday.setBackgroundResource(R.drawable.up);
					buttonSA=0;
				}
			}
		});
        
        mCheck1=(CheckBox)findViewById(R.id.Mirochack);
        System.out.println(String.valueOf(equipment.charAt(0)));
        
        if(Integer.parseInt(String.valueOf(equipment.charAt(0)))==1){
        	System.out.println("set selected");
        	mCheck1.setChecked(true);
        	mCheck1.setTag(1);
        }else{
        	mCheck1.setTag(0);
        }
        mCheck1.setOnCheckedChangeListener(this);
         
        mCheck2=(CheckBox)findViewById(R.id.lightchack);
        if(Integer.parseInt(String.valueOf(equipment.charAt(1)))==1){
        	mCheck2.setChecked(true);
        	mCheck2.setTag(1);
        }else{
        	mCheck2.setTag(0);
        }
        mCheck2.setOnCheckedChangeListener(this);
        
        mCheck3=(CheckBox)findViewById(R.id.musicchack);
        if(Integer.parseInt(String.valueOf(equipment.charAt(2)))==1){
        	mCheck3.setChecked(true);
        	mCheck3.setTag(1);
        }else{
        	mCheck3.setTag(0);
        }
        mCheck3.setOnCheckedChangeListener(this);
        
        mCheck4=(CheckBox)findViewById(R.id.wtheaterchack);
        if(Integer.parseInt(String.valueOf(equipment.charAt(3)))==1){
        	mCheck4.setChecked(true);
        	mCheck4.setTag(1);
        }else{
        	mCheck4.setTag(0);
        }
        mCheck4.setOnCheckedChangeListener(this);
        
        TimePicker01=(TimePicker)findViewById(R.id.timepick);
        if(id!=-1){
        	System.out.println(time.substring(0,2));
//        	mHours=Integer.getInteger(time.substring(0,2));
       	    TimePicker01.setCurrentHour(Integer.parseInt(time.substring(0,2)));
//         	mMinute=Integer.getInteger(time.substring(3,5));
        	TimePicker01.setCurrentMinute(Integer.parseInt(time.substring(3,5)));
        }
        else{
        	TimePicker01.setCurrentHour(c.get(Calendar.HOUR));
        	TimePicker01.setCurrentMinute(c.get(Calendar.MINUTE));
        }

        
        
//        mHours=TimePicker01.getCurrentHour();
//        String strH= mHours>=10?""+mHours:"0"+mHours;
//        mMinute=TimePicker01.getCurrentMinute();
//        String strMu=mMinute>=10?""+mMinute:"0"+mMinute;
//        

        
        Addalarm=(Button)findViewById(R.id.addalarm);
        if(id!=-1){        
        	Addalarm.setText("Change Alarm");
        }
        Addalarm.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				mHours=TimePicker01.getCurrentHour();
		        String strH= mHours>=10?""+mHours:"0"+mHours;
		        mMinute=TimePicker01.getCurrentMinute();
		        String strMu=mMinute>=10?""+mMinute:"0"+mMinute;
		        
		        String Time=strH+strMu;
		        String Week=String.valueOf(buttonMO)+String.valueOf(buttonTUE)+String.valueOf(buttonWEN)
		        		+String.valueOf(buttonTUR)+String.valueOf(buttonFIR)+String.valueOf(buttonSA)
		        		+String.valueOf(buttonSU);
		        System.out.println("week in alarm"+Week);
		        String Equip=String.valueOf(mCheck1.getTag())+String.valueOf(mCheck2.getTag())
		        		+String.valueOf(mCheck3.getTag())+String.valueOf(mCheck4.getTag());
				System.out.println("Time:"+Time);
				System.out.println("Week:"+Week);
				System.out.println("Equp:"+Equip);
				
				if(id==-1){
					System.out.println("add alarm");			
					DatabaseHelper dbHelper= new DatabaseHelper(Alarm.this);
					dbHelper.insertAlarm(new AlarmDB(0, Time, Week, Equip));
					dbHelper.dbClose();
					
					
					
					SetAlarm setalarm=new SetAlarm(Alarm.this);
					//dbHelper.close();
				}
				
				else{
					DatabaseHelper dbHelper= new DatabaseHelper(Alarm.this);
					System.out.println("chang alarm"+id);
					dbHelper.updateAlarm(new AlarmDB(id, Time, Week, Equip));
					dbHelper.dbClose();
					
					SetAlarm setalarm=new SetAlarm(Alarm.this);
				}
				//FindNewAlarm find = new FindNewAlarm(Alarm.this);
				//find.getAlarm();
			}
		});
        
        delAlarm=(Button)findViewById(R.id.deletalarm);
        if(id==-1){
        	delAlarm.setEnabled(false);
        }else{
        	delAlarm.setEnabled(true);
        }
        delAlarm.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DatabaseHelper dbHelper= new DatabaseHelper(Alarm.this);
				dbHelper.deleteAlarm(new AlarmDB(id,null,null,null));
				dbHelper.dbClose();
				
				//SetAlarm setalarm=new SetAlarm(Alarm.this);
				
				Intent intent= new Intent();
				intent.setClass(Alarm.this, AlarmList.class);
				Alarm.this.startActivity(intent);
			}
		});
        		
    }

	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// TODO Auto-generated method stub
//		if(buttonView.equals(mCheck1)){
//			if(isChecked){
//				check1=1;
//			}
//		}
		if(isChecked)buttonView.setTag(1);
		else buttonView.setTag(0);
	}
}
